module.exports = function main(inputs) {
    let datbase=require('./datbase');
    let cartItem=[];
    let object={};
    for(let i of inputs){
      if(i.indexOf('-')==-1){
        if(!object[i]){
            object[i]={};
            object[i].barcode=i;
            object[i].count=1;
        }
        else {
            object[i].count++;
        }
      }
      else {
          let arr=i.split("-");
          object[arr[0]]={}
          object[arr[0]].barcode=arr[0];
          object[arr[0]].count=parseInt(arr[1]);
      }
    }
    for(let i in object){
        cartItem.push(object[i]);
    }
    //console.log(cartItem);
    
    let allItems = datbase.AllItems();
    let Promotions = datbase.Promotions();
    let gifts=[];
    let object1={};
    for(let i of cartItem){
        if(Promotions[0].barcodes.includes(i.barcode)){
            if(i.count>2){
                object1[i.barcode]={};
                for(let j of allItems){
                    if(j.barcode==i.barcode){
                        object1[i.barcode].name=j.name;
                        object1[i.barcode].count=parseInt(i.count/3);
                        object1[i.barcode].unit=j.unit;
                    }
                }
            }
        }
    }
    for(let i in object1){
        gifts.push(object1[i]);
    }
    //console.log(gifts);
    let receiptItems=[];
    let object2={};
    for(let i of cartItem){
        for(let j of allItems){
            if(i.barcode==j.barcode){
                object2[i.barcode]={};
                object2[i.barcode].name=j.name;
                object2[i.barcode].count=i.count;
                object2[i.barcode].unit=j.unit;
                object2[i.barcode].price=(j.price.toFixed(2)).toString();
                let arr1=[];
                for(let k of gifts){
                    arr1.push(k.name);
                }
                if(!arr1.includes(j.name)){
                    subTotal=i.count*j.price; 
                }
                else {
                    for(let l of gifts){
                        if(l.name==j.name){
                           subTotal=(i.count-l.count)*j.price; 
                        }
                    }
                }
                object2[i.barcode].subTotal=(subTotal.toFixed(2)).toString();
            }
        }
    }
    for(let i in object2){
        receiptItems.push(object2[i]);
    }
    //console.log(receiptItems);
    let summary={};
    let sum=0,total=0;    
    for(let i of receiptItems){
        total+=i.subTotal*1;
        sum+=i.count*i.price;
    }
    summary.total=(total.toFixed(2)).toString();
    summary.saved=((sum-summary.total).toFixed(2)).toString();
    //console.log(summary);
    let receipt={};
    receipt.items=receiptItems;
    receipt.gifts=gifts;
    receipt.summary=summary;
    //console.log(receipt);
    console.log(
        '***<没钱赚商店>购物清单***\n' +
        '名称：'+receipt['items'][0].name+'，数量：'+receipt['items'][0].count+receipt['items'][0].unit+'，单价：'+receipt['items'][0].price+'(元)，小计：'+receipt['items'][0].subTotal+'(元)\n' +
        '名称：'+receipt['items'][1].name+'，数量：'+receipt['items'][1].count+receipt['items'][1].unit+'，单价：'+receipt['items'][1].price+'(元)，小计：'+receipt['items'][1].subTotal+'(元)\n' +
        '名称：'+receipt['items'][2].name+'，数量：'+receipt['items'][2].count+receipt['items'][2].unit+'，单价：'+receipt['items'][2].price+'(元)，小计：'+receipt['items'][2].subTotal+'(元)\n' +
        '----------------------\n' +
        '挥泪赠送商品：\n' +
        '名称：'+receipt['gifts'][0].name+'，数量：'+receipt['gifts'][0].count+receipt['gifts'][0].unit+'\n' +
        '名称：'+receipt['gifts'][1].name+'，数量：'+receipt['gifts'][1].count+receipt['gifts'][1].unit+'\n' +
        '----------------------\n' +
        '总计：'+receipt['summary'].total+'(元)\n' +
        '节省：'+receipt['summary'].saved+'(元)\n' +
        '**********************'
    )
};